var $topplaces 	= $('#topplace'),
	$newfeeds 	=  $('#newfeed'),
	$foodcategories = $('#foodcategories'),
	topplaces 	= null,
	topplaceListView	= null,
	newfeeds 	= null,
	newfeedListView 	= null,
	foodcategories	= null,
	foodcateListView = null,
	serverUrl	= 'http://localhost/zigo/server/home.php/';
	
Place = Backbone.Model.extend({
	defaults: {
		place_id: '',
		total_comment: '',
		place_name: '',
		url_image: '',
		stt: ''
	},
	urlRoot: serverUrl + 'topplace'
});

TopPlaceCollection = Backbone.Collection.extend({
	model: Place,
	url: serverUrl + 'topplace'
});

TopPlaceItem = Backbone.View.extend({
	tagName: 'div',
	className: 'topplace-item',
	initialize: function() {
		this.render = _.bind(this.render, this);
		this.template = _.template($('#topplace-item').html());
		//this.model.bind('change', this.render);
	},
	render: function() {
		this.$el.html(this.template(this.model.attributes));
		return this;
	}
});

TopPlaceList = Backbone.View.extend({
	initialize: function() {
		_(this).bindAll('add');

		this._topplace = [];
		
		this.collection.each(this.add);
		
		this.collection.bind('add', this.add);
	},
	render: function() {
		this._rendered = true;
		this.$el.empty();
		_(this._topplace).each(function(item) {
			$topplaces.append(item.render().el);
		});
	},
	add: function(place) {
		var topPlaceItem = new TopPlaceItem({model: place});

		this._topplace.push(topPlaceItem);

		if (this._rendered) {
			this.$el.append(topPlaceItem.render().el);
		}
	}
});

Feed = Backbone.Model.extend({
	defaults: {
		place_id: '',
		detail_comment: '',
		place_name: '',
		user_id_zing: '',
		user_avatar_zing: '',
		user_display_name: '',
		user_name_zing: ''
	},
	urlRoot: serverUrl + 'newfeed'
});

FeedView = Backbone.View.extend({
	tagName: 'div',
	//className: 'topplace-item',
	initialize: function() {
		this.render = _.bind(this.render, this);
		this.template = _.template($('#newfeed-item').html());
		//this.model.bind('change', this.render);
	},
	render: function() {
		this.$el.html(this.template(this.model.attributes));
		return this;
	}
});

FeedCollection = Backbone.Collection.extend({
	model: Feed,
	url: serverUrl + 'newfeed'
});

FeedList = Backbone.View.extend({
	initialize: function() {
		_(this).bindAll('add');

		this._newfeeds = [];
		
		this.collection.each(this.add);
		
		this.collection.bind('add', this.add);
	},
	render: function() {
		this._rendered = true;
		this.$el.empty();
		_(this._newfeeds).each(function(item) {
			$newfeeds.append(item.render().el);
		});
	},
	add: function(feed) {
		var newFeedItem = new FeedView({model: feed});

		this._newfeeds.push(newFeedItem);

		if (this._rendered) {
			this.$el.append(newFeedItem.render().el);
		}
	}
});

FoodCate = Backbone.Model.extend({
	defaults: {
		category_place_id: '',
		category_place_name: ''
	},
	urlRoot: serverUrl + 'foodcategories'
});

FoodCateCollection = Backbone.Collection.extend({
	model: FoodCate,
	url: serverUrl + 'foodcategories'
});

FoodCateViewItem = Backbone.View.extend({
	tagName: 'li',
	className: 'list-group-item',
	initialize: function() {
		this.render = _.bind(this.render, this);
		this.template = _.template($('#foodcate-item').html());
		//this.model.bind('change', this.render);
	},
	render: function() {
		this.$el.html(this.template(this.model.attributes));
		return this;
	}
});
FoodCateList = Backbone.View.extend({
	initialize: function() {
		_(this).bindAll('add');

		this._foodcates = [];
		
		this.collection.each(this.add);
		
		this.collection.bind('add', this.add);
	},
	render: function() {
		this._rendered = true;
		this.$el.empty();
		_(this._foodcates).each(function(item) {
			$foodcategories.append(item.render().el);
		});
	},
	add: function(food) {
		var foodCateItem = new FoodCateViewItem({model: food});

		this._foodcates.push(foodCateItem);

		if (this._rendered) {
			this.$el.append(foodCateItem.render().el);
		}
	}
});

